package com.sinoglobal.ningxia.adapter;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;

import com.sinoglobal.ningxia.R;
import com.sinoglobal.ningxia.beans.ProgramaListBean;
import com.sinoglobal.ningxia.http.ConnectionUtil;
import com.squareup.picasso.Picasso;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by XINDZ on 2017/2/18.
 * 功能:视频模块具体栏目的adapter
 */

public class NewVideoFramentAdapter extends BaseAdapter {
    private Context context;
    private List<ProgramaListBean.ListBean.LanmuBean> beanList = new ArrayList();
    private LayoutInflater layoutInflater;

    public NewVideoFramentAdapter(Context context) {
        this.context = context;
        layoutInflater = LayoutInflater.from(context);
    }

    public NewVideoFramentAdapter(Context context, List beanList) {
        this.context = context;
        this.beanList = beanList;
        layoutInflater = LayoutInflater.from(context);
    }

    public void setData(List beanList) {
        this.beanList = beanList;
        notifyDataSetChanged();
    }

    @Override
    public int getCount() {
        return beanList.size();
    }

    @Override
    public Object getItem(int i) {
        return beanList.get(i);
    }

    @Override
    public long getItemId(int i) {
        return i;
    }

    @Override
    public View getView(int i, View view, ViewGroup viewGroup) {
        ViewHolder viewHolder;
        if (view == null) {
            viewHolder = new ViewHolder();
            view = layoutInflater.inflate(R.layout.item_frag_newsvide, null);
            viewHolder.ivLogo = (ImageView) view.findViewById(R.id.iv_item_newsvideo_logo);
            viewHolder.tvTitle = (TextView) view.findViewById(R.id.tv_item_newvideo_title);
            viewHolder.tvIntro = (TextView) view.findViewById(R.id.tv_item_newvideo_intro);
            viewHolder.tvFlagHot = (TextView) view.findViewById(R.id.tv_item_newvideo_flag_hot);
            viewHolder.tvFlagBC = (TextView) view.findViewById(R.id.tv_item_newvideo_flag_bc);
            viewHolder.tvDate = (TextView) view.findViewById(R.id.tv_item_newvideo_date);
            view.setTag(viewHolder);
        } else {
            viewHolder = (ViewHolder) view.getTag();
        }
        ProgramaListBean.ListBean.LanmuBean lanmuBean = (ProgramaListBean.ListBean.LanmuBean) beanList.get(i);
        String lanmuimage = lanmuBean.getLanmuimage();
        String lanmuname = lanmuBean.getLanmuname();
        String host = lanmuBean.getHost();
        String lanmuid = lanmuBean.getLanmuid();
        String content = lanmuBean.getContent();
        String time_str = lanmuBean.getTime_str();
        String hot = lanmuBean.getHot();
        int bochu = lanmuBean.getBochu();


        viewHolder.tvTitle.setText(lanmuname);
        viewHolder.tvIntro.setText("主持人:" + host);
        if (hot != null && hot.equals("1")) {
            viewHolder.tvFlagHot.setVisibility(View.VISIBLE);
        } else {
            viewHolder.tvFlagHot.setVisibility(View.GONE);
        }
        if (bochu == 1) {
            viewHolder.tvFlagBC.setVisibility(View.VISIBLE);
        } else {
            viewHolder.tvFlagBC.setVisibility(View.GONE);
        }
        viewHolder.tvDate.setText(time_str);
        if (lanmuimage != null && !lanmuimage.equals("")) {
            String url = ConnectionUtil.localUrl + lanmuimage;
            Picasso.with(context).load(url).placeholder(context.getResources().getDrawable(R.drawable.default43))
                    .error(context.getResources().getDrawable(R.drawable.default43))
                    .into(viewHolder.ivLogo);
        }
        return view;
    }

    static class ViewHolder {
        ImageView ivLogo;
        TextView tvTitle, tvIntro, tvDate, tvFlagHot, tvFlagBC;
    }
}
